<template>
  <div class="container">
    <LoginForm v-if="showLoginForm" @login="handleLogin" />
    <RegisterForm v-if="showRegisterForm" @register="handleRegister" />
    <div class="toggle-button" @click="toggleForm">
      <span>{{ showLoginForm ? '切换到注册' : '切换到登录' }}</span>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

import LoginForm from '../components/LoginForm.vue'
import RegisterForm from '../components/RegisterForm.vue'

export default {
  components: {
    LoginForm,
    RegisterForm
  },
  setup() {
    const showLoginForm = ref(true)
    const showRegisterForm = ref(false)
    const router = useRouter()

    const toggleForm = () => {
      showLoginForm.value = !showLoginForm.value
      showRegisterForm.value = !showRegisterForm.value
    }

    const handleLogin = () => {
      // 登录成功后跳转到首页
      router.push('/')
    }

    const handleRegister = () => {
      // 注册成功后隐藏注册表单,显示登录表单
      showRegisterForm.value = false
      showLoginForm.value = true
    }

    return {
      showLoginForm,
      showRegisterForm,
      toggleForm,
      handleLogin,
      handleRegister
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f0f0f0;
}

.toggle-button {
  margin-top: 20px;
  cursor: pointer;
  color: #4CAF50;
  font-weight: bold;
}

.toggle-button:hover {
  color: #45a049;
}
</style>